package com.yingxin.yxerp.module.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yxerp.module.common.ValueLabel;
import com.yingxin.yxerp.module.sys.entity.DingMessageSet;
import com.yingxin.yxerp.module.sys.entity.builder.DingMessageSetBuilder;
import com.yingxin.yxerp.module.sys.entity.enums.DingChatType;
import com.yingxin.yxerp.module.sys.mapper.DingMessageSetMapper;
import com.yingxin.yxerp.module.sys.service.IDingMessageSetService;
import com.yingxin.yxerp.module.sys.vo.req.DingMessageSetCreateVo;
import com.yingxin.yxerp.module.sys.vo.req.DingMessageSetQueryVo;
import com.yingxin.yxerp.module.sys.vo.req.DingMessageSetUpdateVo;
import com.yingxin.yxerp.module.sys.vo.resp.DingMessageSetRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 钉钉发送消息回话id设置 前端控制器
 * @author jiangxudong
 * @since 2022-05-13
 */

@RestController
@RequestMapping("/sys/ding-message-set")
@SuppressWarnings("all")
public class DingMessageSetController {

    @Autowired
    private IDingMessageSetService dingMessageSetService;

    @Autowired
    private DingMessageSetMapper dingMessageSetMapper;

    @ApiOperation(value = "会话类型")
    @GetMapping("/chat-type")
    public List<ValueLabel> listChatType() {
        List<ValueLabel> list = new ArrayList<>();
        for (DingChatType i : DingChatType.values()) {
            ValueLabel value = new ValueLabel();
            value.setValue(i.getType());
            value.setLabel(i.getTypeName());
            list.add(value);
        }
        return list;
    }

    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<DingMessageSetRespVo> page(@RequestBody DingMessageSetQueryVo queryVo) {
        IPage<DingMessageSetRespVo> page = dingMessageSetMapper.pageVo(PageHelper.in(queryVo), queryVo);
        return PageHelper.out(page);
    }

    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<DingMessageSetRespVo> list(@RequestBody DingMessageSetQueryVo queryVo) {
        return dingMessageSetMapper.listVo(queryVo);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public DingMessageSetRespVo get(@PathVariable("id") String id) {
        DingMessageSetRespVo dingMessageSet = dingMessageSetMapper.getVo(id);
        return dingMessageSet;
    }

    @PreAuthorize("hasAuthority('dingMessage:setting:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid DingMessageSetCreateVo createVo) {
        DingMessageSet dingMessageSet = DingMessageSetBuilder.build(createVo);
        dingMessageSetService.create(dingMessageSet);
        return dingMessageSet.getId();
    }

    @PreAuthorize("hasAuthority('dingMessage:setting:update')")
    @ApiOperation(value = "修改")
    @PutMapping
    public String update(@RequestBody @Valid DingMessageSetUpdateVo updateVo) {
        DingMessageSet dingMessageSet = DingMessageSetBuilder.build(updateVo);
        dingMessageSetService.update(dingMessageSet);
        return dingMessageSet.getId();
    }

    @PreAuthorize("hasAuthority('dingMessage:setting:delete')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        DingMessageSet dingMessageSet = dingMessageSetService.getById(id);
        dingMessageSetService.delete(dingMessageSet);
    }

}
